home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / programming / libraries / newiff.lha / NewIFF / NewIFF39.lha / newiff39 / modules / getbitmap.c < prev    next >
C/C++ Source or Header  |  1993-09-28  |  4KB  |  154 lines

  1.  
  2. /*----------------------------------------------------------------------*
  3.  * GETBITMAP.C  Support routines for reading ILBM files.
  4.  * (IFF is Interchange Format File.)
  5.  *
  6.  * Based on code by Jerry Morrison and Steve Shaw, Electronic Arts.
  7.  * This software is in the public domain.
  8.  * Modified for iffparse.library by CBM 04/90
  9.  * This version for the Commodore-Amiga computer.
  10.  *
  11.  * 09/15/92 - conditionally use AllocBitMap instead of AllocRaster
  12.  * 39.5 (11/92) - use RowBits() macro name instead of BitsPerRow()
  13.  *----------------------------------------------------------------------*/
  14.  
  15. #include "iffp/ilbm.h"
  16. #include "iffp/packer.h"
  17. #include "iffp/ilbmapp.h"
  18.  
  19. /* createbrush
  20.  *
  21.  * Passed an initialized ILBMInfo with a parsed IFFHandle (chunks parsed,
  22.  * stopped at BODY),
  23.  * gets the bitmap and colors
  24.  * Sets up ilbm->brbitmap, ilbm->colortable, ilbm->ncolors
  25.  * Returns 0 for success
  26.  */
  27. LONG createbrush(struct ILBMInfo *ilbm)
  28.     {
  29.     int error;
  30.  
  31.     error             = getbitmap(ilbm);
  32.     if(!error) error     = loadbody(ilbm->ParseInfo.iff,
  33.                         ilbm->brbitmap,&ilbm->Bmhd);
  34.     if(!error)         getcolors(ilbm);
  35.     if(error)          deletebrush(ilbm);
  36.     return(error);
  37.     }
  38.  
  39. /* deletebrush
  40.  *
  41.  * closes and deallocates created brush bitmap and colors
  42.  */
  43. void deletebrush(ilbm)
  44. struct ILBMInfo        *ilbm;
  45.     {
  46.     freebitmap(ilbm);
  47.     freecolors(ilbm);
  48.     }
  49.  
  50.  
  51. /* getbitmap
  52.  *
  53.  * Passed an initialized ILBMInfo with parsed IFFHandle (chunks parsed,
  54.  * stopped at BODY), allocates a BitMap structure and planes just large
  55.  * enough for the BODY.  Generally used for brushes but may be used
  56.  * to load backgrounds without displaying, or to load deep ILBM's.
  57.  * Sets ilbm->brbitmap.  Returns 0 for success.
  58.  */
  59. LONG getbitmap(struct ILBMInfo *ilbm)
  60.     {
  61.     struct IFFHandle    *iff;
  62.     BitMapHeader    *bmhd;
  63.     USHORT            wide, high;
  64.     LONG  error = NULL;
  65.     int k, extra=0;
  66.     BYTE deep;
  67.  
  68.     if(!(iff=ilbm->ParseInfo.iff))    return(CLIENT_ERROR);
  69.  
  70.     ilbm->brbitmap = NULL;
  71.  
  72.     if (!( bmhd = (BitMapHeader *)
  73.             findpropdata(iff, ID_ILBM, ID_BMHD)))
  74.         {
  75.         message(SI(MSG_ILBM_NOBMHD));
  76.         return(IFFERR_SYNTAX);
  77.         }
  78.  
  79.     *(&ilbm->Bmhd) = *bmhd;    /* copy contents of BMHD */
  80.  
  81.     wide = RowBits(bmhd->w);
  82.     high = bmhd->h;
  83.     deep = bmhd->nPlanes;
  84.  
  85.     ilbm->camg = getcamg(ilbm);
  86.  
  87.     D(bug("allocbitmap: bmhd=$%lx wide=%ld high=%ld deep=%ld\n",
  88.             bmhd,wide,high,deep));
  89.     /*
  90.      * Allocate Bitmap and planes
  91.      */
  92.         extra = deep > 8 ? deep - 8 : 0;
  93.  
  94.     if(GfxBase->lib_Version >= 39)
  95.         {
  96.         if(!(ilbm->brbitmap = 
  97.         AllocBitMap(wide,high,deep,BMF_DISPLAYABLE|BMF_CLEAR, NULL)))
  98.             error = IFFERR_NOMEM;
  99.         }
  100.     else if(ilbm->brbitmap = AllocMem(sizeof(struct BitMap) + (extra<<2),MEMF_CLEAR))
  101.         {
  102.         InitBitMap(ilbm->brbitmap,deep,wide,high);
  103.         for(k=0; k<deep && (!error); k++) 
  104.         {
  105.         if(!(ilbm->brbitmap->Planes[k] = AllocRaster(wide,high))) error = 1;
  106.         if(! error)
  107.             BltClear(ilbm->brbitmap->Planes[k],RASSIZE(wide,high),0);
  108.         }
  109.         }
  110.     else error = IFFERR_NOMEM;
  111.  
  112.     if(error)    
  113.         {
  114.         freebitmap(ilbm);
  115.         message(SI(MSG_ILBM_NORASTER));
  116.         }
  117.  
  118.     return(error);
  119.     }
  120.  
  121.     
  122. /* freebitmap
  123.  *
  124.  * deallocates ilbm->brbitmap BitMap structure and planes 
  125.  */
  126. void freebitmap(struct ILBMInfo * ilbm)
  127.     {
  128.     int k, extra=0;
  129.  
  130.     if(ilbm->brbitmap)
  131.         {
  132.         if(GfxBase->lib_Version >= 39)
  133.         {
  134.         FreeBitMap(ilbm->brbitmap);
  135.         }
  136.         else
  137.         {
  138.         for(k=0; k< ilbm->brbitmap->Depth; k++) 
  139.             {
  140.             if(ilbm->brbitmap->Planes[k]) 
  141.             FreeRaster(ilbm->brbitmap->Planes[k],
  142.                        (USHORT)(ilbm->brbitmap->BytesPerRow << 3),
  143.                        ilbm->brbitmap->Rows);
  144.             }
  145.  
  146.                 extra = ilbm->brbitmap->Depth > 8 ? ilbm->brbitmap->Depth - 8 : 0;
  147.             FreeMem(ilbm->brbitmap,sizeof(struct BitMap) + (extra << 2));
  148.         }
  149.         ilbm->brbitmap = NULL;
  150.         }
  151.     }
  152.  
  153. /* end */
  154.